package com.example.mp3player01;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.SeekBar;

import com.example.model.Mp3Info;

public class PlayerActivity extends Activity {
	
	ImageButton beginButton;
	ImageButton resumeButton;
	ImageButton stopButton;
	static MediaPlayer mediaPlayer;
	ImageButton shareButton;
	SeekBar seekbar;
	
	private final Handler handler = new Handler();
	
	private boolean isPlaying = false;
	private boolean isPause = false;
	private boolean isReleased = false;
	
	private Mp3Info mp3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.player);
		
		Intent intent = getIntent();
		mp3 = (Mp3Info) intent.getSerializableExtra("mp3Info");
		beginButton = (ImageButton) findViewById(R.id.begin);
		beginButton.setOnClickListener(new BeginButtonListener());
		resumeButton = (ImageButton) findViewById(R.id.resume);
		resumeButton.setOnClickListener(new ResumeButtonListener());
		stopButton = (ImageButton) findViewById(R.id.stop);
		stopButton.setOnClickListener(new StopButtonListener());
		
		shareButton = (ImageButton) findViewById(R.id.share);
		shareButton.setOnClickListener(new ShareOnFacebookButtonListener());
		
		seekbar = (SeekBar) findViewById(R.id.seekBar1);
		
		
		
	}

    // This is event handler thumb moving event
    private void seekChange(View v){
    	if(mediaPlayer.isPlaying()){
	    	SeekBar sb = (SeekBar)v;
			mediaPlayer.seekTo(sb.getProgress());
		}
    }
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		mediaPlayer.release();
	}

	class BeginButtonListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (!isPlaying) {
				String path = getMp3Path(mp3);
				mediaPlayer = MediaPlayer.create(PlayerActivity.this, Uri.parse("file://" + path));
				
				seekbar.setMax(mediaPlayer.getDuration());
				seekbar.setOnTouchListener(new OnTouchListener() {
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						seekChange(v);
						return false; 
					}
				});
				
				
				mediaPlayer.setLooping(false);
				mediaPlayer.start();
				
				startPlayProgressUpdater();
				
				isPlaying = true;
				isReleased = false;
				isPause = false;
			}
		}
	}
	
	public void startPlayProgressUpdater() {
    	seekbar.setProgress(mediaPlayer.getCurrentPosition());

		if (mediaPlayer.isPlaying()) {
			Runnable notification = new Runnable() {
		        public void run() {
		        	startPlayProgressUpdater();
				}
		    };
		    handler.postDelayed(notification,1000);
    	}else{
    		mediaPlayer.pause();
    		isPlaying = false;
//    		buttonPlayStop.setText(getString(R.string.play_str));
    		seekbar.setProgress(0);
    	}
    }
	
	class ResumeButtonListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (mediaPlayer != null && !isReleased) {
				if (!isPause) {
					mediaPlayer.pause();
					isPlaying = false;
					isReleased = false;
					isPause = true;
				} else {
					mediaPlayer.start();
					isPlaying = true;
					isReleased = false;
					isPause = false;
				}
			}
		}
	}
	
	class StopButtonListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (mediaPlayer != null) {
				if (isPlaying) {
					if (!isReleased) {
						mediaPlayer.stop();
						mediaPlayer.release();
						isPlaying = false;
						isReleased = true;
						isPause = false;
					}
				}
			}
		}
	}
	class ShareOnFacebookButtonListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(PlayerActivity.this, FBLoginActivity.class);
			startActivity(intent);
		}
	}
	
	private String getMp3Path(Mp3Info mp3Info) {
		String SDCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
		String path = SDCardRoot + File.separator + "mp3" + File.separator + mp3Info.getMp3Name();
		return path;
	}
	
}
